/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lmx.repoDomain.repository;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.lmx.common.util.CommonUtil;
import com.lmx.persistence.service.CommonService;
import com.lmx.persistence.service.CustomerService;
import com.lmx.repoDomain.tmodel.CustomerDevice;
import com.lmx.repoDomain.tmodel.CustomerDrug;
import com.lmx.repoDomain.tmodel.Device;
import com.lmx.common.constants.CommonConstants;
import io.katharsis.repository.annotations.JsonApiResourceRepository;
import io.katharsis.repository.annotations.JsonApiSave;

@JsonApiResourceRepository(CustomerDevice.class)
@Component
public class CustomerDeviceRepository extends BaseRepository<CustomerDevice> {

	@Autowired
	protected CommonService commonService;

	@Autowired
	protected CustomerService customerService;

	@PostConstruct
	public void init() {
		dslJpaService.setEntityClass(CustomerDevice.class);
	}

	@Override
	@JsonApiSave
	public CustomerDevice save(CustomerDevice data) {
		List deviceList = new ArrayList();
		if (data.getId() == null) {
			Device device = data.getDevice();
			device.setStatus(commonService.getDicttype(CommonConstants.DICTTYPE.DEVICE_STATUS_2.label()));// 占用
			deviceList.add(device);
			//  查询此老人 对应该类设备  
			CustomerDevice oldData=customerService.getCustomerDeviceByDeviceType(data);
			if(oldData!=null&&oldData.getDevice()!=null){//  如果已存在 
				oldData.setDelStatus((short)1);
				dslJpaService.update(oldData);//删除原有此类设备的绑定数据
				if(oldData.getDevice()!=null&&!oldData.getDevice().getId().equals(data.getDevice().getId())){
					Device oldDevice=oldData.getDevice();
					oldDevice.setStatus(commonService.getDicttype(CommonConstants.DICTTYPE.DEVICE_STATUS_1.label()));//  将老设备置为空闲
					deviceList.add(oldDevice);
				}
			}
			dslJpaService.create(data);
		} else {
			Device deviceOld = customerService.getCustomerDeviceById(data).getDevice();
			Device deviceNew = data.getDevice();
			if(deviceOld!=null&&!deviceOld.getId().equals(deviceNew.getId())){
				deviceOld.setStatus(commonService.getDicttype(CommonConstants.DICTTYPE.DEVICE_STATUS_1.label()));// 空闲
				deviceOld.setLastUpdateDateTime(CommonUtil.getCurrTimes());
				deviceList.add(deviceOld);
			}
			if(data.getDelStatus()==1){
				deviceNew.setStatus(commonService.getDicttype(CommonConstants.DICTTYPE.DEVICE_STATUS_1.label()));
			}else{
				deviceNew.setStatus(commonService.getDicttype(CommonConstants.DICTTYPE.DEVICE_STATUS_2.label()));// 占用
			}
			deviceNew.setLastUpdateDateTime(CommonUtil.getCurrTimes());
			deviceList.add(deviceNew);
			dslJpaService.update(data);
		}
		commonService.batchUpdate(deviceList);// 更新 设备状态
		return data;
	}

}
